﻿//
//调用接口服务
//
(function($) {
    $.fn.ExecuteCommand = function(commandSetting, key, param) {
        var that = this;
        //判断是否正在执行命令
        if (that.attr('loading_command') == '1') {
            return;
        };
        var setting = {
            //服务地址
            serviceUrl: '/Interface/ajax/CommandService.aspx',
            //是否需要用户确认
            needConfirm: false,
            confirmMsg: '确定执行此操作?',
            //执行成功回调
            successCallBack: function(result, totalCount) { },
            //执行错误回调
            errorCallback: function(errorCode, msg) { }

        };
        $.extend(setting, commandSetting);

        if (!setting.needConfirm || (setting.needConfirm && confirm(setting.confirmMsg))) {
            that.attr('loading_command', '1');
            $.ajax({
                url: setting.serviceUrl,
                data: { key: key, param: $.toJSON(param), temp: new Date().valueOf() },
                type: 'POST',
                dataType: 'json',
                cache: false,
                error: function(data) {
                    that.attr('loading_command', '0');
                    setting.errorCallback('9999', 'ajax获取数据发生错误，错误代码:' + data.status);
                },
                success: function(result) {
                    that.attr('loading_command', '0');
                    if (result.Error) {
                        setting.errorCallback(result.ErrorCode, result.ErrorMsg);
                    } else {
                        setting.successCallBack(result.Result, result.TotalCount);
                    }
                }
            });
        }

    }

})(jQuery);

//
//format
//var template1 = "我是{0}，今年{1}了";
//var template2 = "我是{name}，今年{age}了";
//var result1=template1.format("loogn",22);
//var result2 = template2.format({ name: "loogn", age: 22 });
//两个结果都是"我是loogn，今年22了"
//
String.prototype.format = function(args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

//
//获取时间
//
Date.prototype.getComfortableString = function() {
    var str = this.getFullYear() + '-' + this.getMonth() + '-' + this.getDate();
    var now = new Date();
    
    if (this > new Date(now.getFullYear(), now.getMonth(), now.getDate())) {
        str = '今天';
    }
    else if (this > new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)) {
        str = '昨天';
    } 
    else if (this > new Date(now.getFullYear(), now.getMonth(), now.getDate() - 2)) {
        str = '前天';
    }
    return str + ' ' + this.getHours() + ':' + this.getMinutes();
}

function showDialogPage(title, content) {
    $('#dialogPage').find('div[data-role=header] h2').html(title);
    $('#dialogPage').find('div.ui-content p').html(content);
    $("#dialogPage").popup("open");
}

//
// 带表单验证
//
function ExecuteFormCommonCommand(formid, callback) {
    $form = $("#" + formid);
    $form.trigger("validate");
    if ($form.isValid()) {
        ExecuteCommonCommand($form, callback);
    }
}

function ExecuteCommonCommand(ui, callback) {
    var param = {};
    ui.find('[command-param]').each(function () {
        if ($(this).attr('data-role') == 'controlgroup') {
            var val = '';
            $(this).find('input').each(function () {
                if (this.type == "radio") {
                    if ($(this)[0].checked == true) {
                        val = $(this).val();
                    }
                } else if (this.type == "checkbox") {
                    if ($(this)[0].checked == true) {
                        val += $(this).val() + ",";
                    }
                }
            });
            param[$(this).attr('command-param')] = val;
        } else {
            param[$(this).attr('command-param')] = $(this).val();
        }
    });
    ui.ExecuteCommand({
        successCallBack: function (result) {
            if (typeof (callback) == "function") {
                callback(result);
            } else if (typeof (callback) == "string") {
                eval(callback + "(result)");
            }
            else {
                showDialogPage('成功', '操作成功');
            }
        },
        errorCallback: function (errorCode, msg) {
            showDialogPage('错误', msg);
        }
    }, ui.attr('command'), param);
}